package com.rankeiot.platform.controller;

//import com.aliyun.dysmsapi20170525.Client;
import com.rankeiot.core.anno.Auth;
import com.rankeiot.core.data.response.Resp;
import com.rankeiot.core.inner.argumet.QueryPage;
import com.rankeiot.core.sqltoy.Sql;
import com.rankeiot.core.util.JSONUtil;

import com.rankeiot.platform.SmsMenu;
import com.rankeiot.platform.domain.SmsMessage;
import com.rankeiot.platform.service.SmsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.hibernate.validator.constraints.Length;
import org.sagacity.sqltoy.dao.SqlToyLazyDao;
import org.sagacity.sqltoy.model.Page;
import org.springframework.web.bind.annotation.*;


import java.util.Map;

/**
 * 短信信息
 */
@Tag(name = "短信信息")
@Auth
@RestController
@RequestMapping("smsMessage")
@RequiredArgsConstructor
//@ConditionalOnClass(Client.class)
public class SmsMessageController {

    final SqlToyLazyDao dao;
    final SmsService smsService;

    @Schema(description = "短信消息")
    @Data
    public static class MsgSendVo {
        @Schema(description = "手机号码")
        @NotNull
        @Length(max = 20)
        String phone;
        @Schema(description = "模板签名")
        @NotNull
        @Length(max = 20)
        String signName;
        @Schema(description = "模板编号")
        @NotNull
        @Length(max = 20)
        String templateCode;
        @Schema(description = "模板参数")
        @NotNull
        @Length(max = 20)
        String templateParam;
    }

    /**
     * 新增短信信息
     */
    @Operation(summary = "新增短信信息")
    @Auth(SmsMenu.SMSMESSAGE_SEND)
    @PostMapping("send")
    public Resp add(@Valid @RequestBody MsgSendVo sms) {
        smsService.sendMessage(sms.getSignName(), sms.getPhone(), sms.getTemplateCode(), JSONUtil.parseObject(sms.getTemplateParam()));
        return Resp.ok();
    }


    /**
     * 获取短信信息
     */
    @Operation(summary = "获取短信信息详细")
    @Auth(SmsMenu.SMSMESSAGE)
    @GetMapping("detail")
    public Resp<SmsMessage> detail(Integer id) {
        SmsMessage smsMessage = dao.load(new SmsMessage(id));
        return Resp.of(smsMessage);
    }

    /**
     * 获取短信信息列表
     */
    @Operation(summary = "列出短信信息")
    @Auth(SmsMenu.SMSMESSAGE)
    @PostMapping("list")
    public Resp<Map> list(QueryPage query) {
        String sql = Sql.select("*")
                .from(SmsMessage.TABLE)
                .optional(q -> q
                        .eq(SmsMessage.Fields.phoneNumber)
                        .eq(SmsMessage.Fields.templateCode)
                ).orderByDesc(SmsMessage.Fields.id)
                .toString();
        Page result = dao.findPageBySql(query.page(), sql, query.getParams());
        return Resp.of(result);
    }
    @Operation(summary = "列出签名信息")
    @Auth(SmsMenu.SMSMESSAGE)
    @GetMapping("signs")
    public Resp listSign(){
        return Resp.of(smsService.getSignList());
    }
    @Operation(summary = "列出模板信息")
    @Auth(SmsMenu.SMSMESSAGE)
    @GetMapping("templates")
    public Resp listTemplates(){
        return Resp.of(smsService.getTemplateList());
    }
}
